﻿/* ********************************************************************************
 * 名称：MngrLog_RepositoryService
 * 描述：MngrLog  仓储服务接口实现类（本类通过CodeSmith软件生成生成）
 * 创建人：HFL
 * 创建日期：2020-08-21 14:47:29
 * 修改人：
 * 修改日期：
 * *******************************************************************************/
using WPay.Domain.MainModule.Interface.RepositoryServices;
using WPay.Domain.Model.DTOs;
using WPay.Domain.Model.Entities;
using WPay.Infrastructure.Repository.Base;
using WPay.Infrastructure.Repository.DapperCore;
using WPay.Infrastructure.Unity;
using WPay.Domain;
using WPay.Infrastructure.EFCore;
using WPay.Domain.Model.DTOs.Parms;
using Dapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data.Entity;
using WPay.Infrastructure.Unity.Expand;

namespace WPay.Infrastructure.Repository.MainModule
{
    /// <summary>
    /// MngrLog 仓储服务接口实现类
    /// </summary>
    public class MngrLog_RepositoryService : RepositoryBase, IMngrLog_RepositoryService
    {
        /// <summary>
        /// 查询SQL语句
        /// </summary>
        private readonly string querySql = @"select ID,UId,CreateTime,Contents,IpAddress,IsDeleted,Titles from MngrLog where 1=1";
        /// <summary>
        /// 查询数据条数SQL语句
        /// </summary>
        private readonly string queryCountSql = @"select count(1) from MngrLog where 1=1";
        /// <summary>
        /// 新增SQL语句
        /// </summary>
        private readonly string addSql = @"insert into MngrLog(ID,UId,CreateTime,Contents,IpAddress,IsDeleted,Titles) values (@ID,@UId,@CreateTime,@Contents,@IpAddress,@IsDeleted,@Titles)";
        /// <summary>
        /// 修改所有字段SQL语句
        /// </summary>
        private readonly string editSql = @"update MngrLog set UId=@UId,CreateTime=@CreateTime,Contents=@Contents,IpAddress=@IpAddress,IsDeleted=@IsDeleted,Titles=@Titles where 1=1";
        /// <summary>
        /// 表主键where条件SQL语句
        /// </summary>
        private readonly string whereByPrimaryKey = @" and ID=@ID";

        private readonly IEfCore_RepositoryService _efCore_RepositoryService;
        private Entities db;
        public MngrLog_RepositoryService(IEfCore_RepositoryService efCore_RepositoryService)
        {
            _efCore_RepositoryService = efCore_RepositoryService;
            db = _efCore_RepositoryService.DbContext();
        }

        #region 由软件生成，禁止修改，若需要扩展，请在下方进行扩展

        /// <summary>
        /// 根据主键获取MngrLog资料信息
        /// </summary>
        /// <param name="_iD"></param>
        /// <returns>返回单个MngrLog数据实体</returns>
        public MngrLog Get(string _iD)
        {
            string sql = querySql + whereByPrimaryKey;
            DynamicParameters param = new DynamicParameters();
            param.Add("ID", _iD);
            return base.QueryBySql<MngrLog>(sql, param);
        }

        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <param name="_cpnID">企业代码</param>
        /// <returns>返回MngrLog数据集合</returns>
        public List<MngrLog> GetAll(string _cpnID)
        {
            return base.QueryListBySql<MngrLog>(querySql + " and CpnID=@CpnID", new { CpnID = _cpnID });
        }

        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <param name="_param">查询条件字典，其中key必须使用字段名</param>
        /// <returns>返回MngrLog数据集合</returns>
        public List<MngrLog> GetAll(Hashtable _param)
        {
            //查询SQL语句组装
            string sql = querySql + base.GetQueryConditions(_param);
            //封装参数
            DynamicParameters dynamicParam = new DynamicParameters();
            foreach (string key in _param.Keys)
            {
                dynamicParam.Add(key, _param[key]);
            }

            return base.QueryListBySql<MngrLog>(sql, dynamicParam);
        }

        /// <summary>
        /// 按照分页条件查询数据集
        /// </summary>
        /// <param name="_param">查询条件字典，其中key必须使用字段名</param>
        /// <param name="_pageIndex">查询页码，从1开始，必传</param>
        /// <param name="_pageSize">每页查询数据条数，必传</param>
        /// <param name="_sort">排序列名，若要降序请用列名+" DESC"，必传</param>
        /// <param name="_pageCount">返回总页码数</param>
        /// <param name="_dataCount">返回总数据条数</param>
        /// <returns>返回MngrLog数据集合</returns>
        public List<MngrLog> GetByPage(Hashtable _param, int _pageIndex, int _pageSize, string _sort, out int _pageCount, out int _dataCount)
        {
            if (_pageIndex == 0 || _pageSize == 0 || string.IsNullOrWhiteSpace(_sort))
            {
                _dataCount = 0;
                _pageCount = 0;
                return null;
            }
            //封装参数
            DynamicParameters dynamicParam = new DynamicParameters();
            foreach (string key in _param.Keys)
                dynamicParam.Add(key, _param[key]);
            //组装计数sql语句
            string countSql = queryCountSql + base.GetQueryConditions(_param);
            _dataCount = base.QueryScalarBySql<Int32>(countSql, dynamicParam);//获取总数据条数
            _pageCount = (_dataCount + _pageSize - 1) / _pageSize;//总页数 =  (数据总条数 + 每页显示条数 - 1) / 每页显示条数
            if (_dataCount == 0)
            {
                return new List<MngrLog>();
            }
            //组装分页查询语句
            string sql = querySql + base.GetQueryConditions(_param, _pageIndex, _pageSize, _sort);

            return base.QueryListBySql<MngrLog>(sql, dynamicParam);
        }

        /// <summary>
        /// 插入MngrLog数据
        /// </summary>
        /// <param name="_mngrLog">需要插入的MngrLog数据实体</param>
        /// <returns>返回影响数据行数</returns>
        public int Add(MngrLog _mngrLog)
        {
            return base.ExecuteBySql(addSql, _mngrLog);
        }

        /// <summary>
        /// 批量插入MngrLog数据
        /// </summary>
        /// <param name="_mngrLogList">需要插入的MngrLog数据集合</param>
        /// <returns>返回影响数据行数</returns>
        public int Add(List<MngrLog> _mngrLogList)
        {
            return base.BulkInsert(_mngrLogList);
        }

        /// <summary>
        /// 根据主键更新MngrLog数据
        /// </summary>
        /// <param name="_mngrLog">需要更新的MngrLog数据实体</param>
        /// <returns>返回影响数据行数</returns>
        public int Update(MngrLog _mngrLog)
        {
            return base.ExecuteBySql(editSql + whereByPrimaryKey, _mngrLog);
        }

        /// <summary>
        /// 根据主键更新MngrLog指定字段
        /// </summary>
        /// <param name="_mngrLog">需要更新的MngrLog数据实体</param>
        /// <param name="_updateProNames">需要更新的字段名</param>
        /// <returns>返回影响数据行数</returns>
        public int Update(MngrLog _mngrLog, params string[] _updateProNames)
        {
            string sql = @"update MngrLog set ";
            //主键值在修改时，禁止为空
            if (_mngrLog.ID == default(string))
                return 0;
            sql += base.GetUpdateSet(_updateProNames);
            return base.ExecuteBySql(sql + whereByPrimaryKey, _mngrLog);
        }

        #endregion

        /*业务扩展代码*/
        #region 业务扩展代码
        public EntityList<MngrLogDTO> GetMngrLogLst(int page, int rows, MngrLogParms mngrlogParms)
        {
            Dictionary<string, string> sort = new Dictionary<string, string>() { { "CreateTime", "DESC" } };
            var where = PredicateBuilder.True<MngrLogDTO>();
            if (!string.IsNullOrEmpty(mngrlogParms.Contents))
            {
                where = where.And(x => x.Contents.Contains(mngrlogParms.Contents));
            }
            var query = (from a in db.MngrLog
                         join b in db.MngrBaseInfo on a.UId equals b.ID
                         select new MngrLogDTO
                         {
                             ID = a.ID,
                             UId = a.UId,
                             CreateTime = a.CreateTime,
                             Contents = a.Contents,
                             IpAddress = a.IpAddress,
                             Titles = a.Titles,
                             NickName = b.NickName,
                             LoginName = b.LoginName
                         }).Where(where).AsNoTracking();
            var result = new EntityList<MngrLogDTO>();
            result = _efCore_RepositoryService.FindAllAsQuery<MngrLogDTO>(_efCore_RepositoryService.DataSort(query, sort), page, rows);
            result.Data.ForEach(x =>
            {
                x.CreateTimeStr = x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
            });
            return result;

        }
        #endregion
    }
}
